﻿using System;
using Quartz;

namespace ZilLion.TaskManager.Core.Quartz
{
    public class CustomJobDetailImpl : IJobDetail
    {
        public object Clone()
        {
            CustomJobDetailImpl copy;
            try
            {
                copy = (CustomJobDetailImpl) MemberwiseClone();
                if (JobDataMap != null)
                    copy.JobDataMap = (JobDataMap) JobDataMap.Clone();
            }
            catch (Exception)
            {
                throw new Exception("Not Cloneable.");
            }

            return copy;
        }

        public JobKey Key { get; }
        public string Description { get; }
        public Type JobType { get; }
        public JobDataMap JobDataMap { get; set; }=new JobDataMap();
        public bool Durable { get; }
        public bool PersistJobDataAfterExecution { get; }
        public bool ConcurrentExecutionDisallowed { get; }
        public bool RequestsRecovery { get; }

        public JobBuilder GetJobBuilder()
        {
            var b = JobBuilder.Create()
                .OfType(JobType)
                .RequestRecovery(RequestsRecovery)
                .StoreDurably(Durable)
                .UsingJobData(JobDataMap)
                .WithDescription(Description)
                .WithIdentity(Key);

            return b;
        }
    }
}